<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Video_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getMyVideoList($fields = ' * ',$per_page, $limit, $filter)
    {
    	$where = " ";
        if(isset($filter['departmentid']) && $filter['departmentid']){
            $where .= " AND departmentid = {$filter['departmentid']}";
        }
    	$limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$fields} FROM tr_video WHERE isDel=0 {$where}  ORDER BY addtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getMyVideoListCnt($filter)
    {
        $where = " ";
        if($filter['departmentid']){
            $where .= " AND departmentid = {$filter['departmentid']}";
        }
        $sql = "SELECT id FROM tr_video WHERE isDel=0 {$where} ORDER BY addtime DESC";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    public function get_cat_list(){
        $this->db->order_by("sort","desc");
        $query = $this->db->get("tr_department");
        return $query->result_array();
    }
    public function getDetail($id){
        $this->db->where("id",$id);
        $query = $this->db->get("tr_video");
        return $query->row_array();
    }
    public function getPlayDetail($id){
        $this->db->where(["videoid"=>$id,'AccountID'=>$_SESSION['userInfo']['AccountID']]);
        $query = $this->db->get("tr_video_record");
        return $query->row_array();
    }

    public function getMyRecordList(){
        $sql = "SELECT v.*,r.duration,r.playtime,r.updatetime FROM tr_video v , tr_video_record r WHERE v.id=r.videoid AND r.AccountID={$_SESSION['userInfo']['AccountID']}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function updataTime($videoid, $params)
    {
        $this->db->where(['AccountID'=>$params['AccountID'],"videoid"=>$videoid]);
        $record_query = $this->db->get("tr_video_record");
        $record_res = $record_query->row_array();
        $res = 0;
        if($record_res){
            if($params['playtime']>$record_res['playtime'])
            {

                unset($params['duration']);
                unset($params['AccountID']);
                $params['updatetime'] = time();
                print_r($params);
                $this->db->where("videoid",$videoid);
                
                $res = $this->db->update('tr_video_record',$params);
            }
            
        }else{
            $params['updatetime'] = time();
            $params['createtime'] = time();
            $params['videoid'] = $videoid;
            $res = $this->db->insert('tr_video_record',$params);
        }
         return $res;
    }
   
    
}

/* End of file Document_model.php */
/* Location: ./application/models/document_model.php */
